// pages/achievement/index/index.js
const wxCharts = require('../../../../utils/wx-charts')
var mychart
const app = getApp()
import { handletabChange } from '../../../utils/Tools'
import moment from '../../../../utils/moment'
import { getDriverAchi } from '../../../network/process'
Page({
  data: {
    date: '',
    car_team:'',
    categories:'',
    num:'',
    series:'',
    list:[],
    maxi:'500',
    imageWidth:'',
    isIPhone: false
  },
  onLoad: function (options) {
    wx.hideHomeButton()
    const imageWidth = wx.getSystemInfoSync().windowWidth / 375;
    const nowTime = moment(new Date(),'YYYY-MM')
    this.setData({
      imageWidth,
      date: nowTime,
      list: app.globalData.list,
      isIPhone: app.globalData.isIPhone
    })
    this._getDriverAchi()
    var dataLength = parseInt(Math.random() * 20);
    console.log(dataLength)
  },
  touchstart(e){
    // console.log(e)
    mychart.scrollStart(e)
  },
  touchmove:function(e){
    // mychart.scroll(e);
    // console.log(e)
    mychart.scroll(e);
    },
    touchend:function(e){
      // console.log(e)
      mychart.scrollEnd(e);
      // mychart.scrollEnd(e);
      },
  //     事件函数       //
  tabChange(e) {
    const {index} = e.detail
    handletabChange(index)
  },
  handleChangeDate(e){
    const {value} = e.detail
    // const date = value.replace("-",".")
    this.setData({
      date:value
    })
    this._getDriverAchi()
  },
  _wxCharts(){
    // console.log(this.data.categories)
    if(this.data.categories.length>0){
    mychart=new wxCharts({
      canvasId: 'chart',
      type: 'line',
      dataLabel: false,
      categories: this.data.categories,
      series:this.data.series,
      enableScroll:true,
      dataPointShape: true,
      axisLabel :{  
        interval:0   
      }, 
      // xAxis: {
      //   axisLabel :{  
      //     interval:0   
      //   }, 
      // },
      yAxis: {
        max: this.data.maxi,
        min: 0
      },
      width: (320 * this.data.imageWidth),
      height: 300
    });
  }
  },
  //      网络请求      //
  async _getDriverAchi(){
    const res = await getDriverAchi(this.data.date)
    const {car_team,categories,num,series} = res.data
    //    拿到图表数据的最大值
    const maxi = Math.max.apply(null,series[0].data)
    if(res.code === 200){
      this.setData({
        car_team,
        categories,
        num,
        series,
        maxi:Number(maxi)+100
      })
      if(this.data.series) this._wxCharts()
    }
  }
})